package com.cx.base.day13_io.io_autoTest;

/**
 * @Author: robert.song
 * @Date: Created in 2021/11/25
 */

import java.io.*;
import java.util.Scanner;

//Scanner中nextLine()方法和next()方法的区别
public class ScanString {
    public static void main(String[] args) throws UnsupportedEncodingException {
        scan();
        tesr2();
    }

    private static void scan() throws UnsupportedEncodingException {
        Scanner input = new Scanner(System.in);
        System.out.println("请输入字符串（nextLine）：");
        String str1 = input.nextLine();
        System.out.println(str1 + ":" + str1.equals(new String(str1.getBytes(), "UTF8")));

        System.out.println("请输入字符串（next）：");
        String str = input.next();
        System.out.println(str);
    }

    public static void tesr2() {

        InputStreamReader isr = null;
        OutputStreamWriter osw = null;
        try {
            File file1 = new File("D://hello.txt");
            File file2 = new File("D://hello_gbk.txt");

            FileInputStream fis = new FileInputStream(file1);
            FileOutputStream fos = new FileOutputStream(file2);

            isr = new InputStreamReader(fis, "utf-8");
            osw = new OutputStreamWriter(fos, "gbk");

            char[] cbuf = new char[1024];
            int len;
            while ((len = isr.read(cbuf)) != -1) {
                for (int i = 0; i < len; i++) {
                    osw.write(cbuf[i]);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (isr != null)
                    isr.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (osw != null)
                    osw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
